package com.quyeying.controller;

import cn.hutool.json.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.quyeying.annotation.PassToken;
import com.quyeying.service.TblOrderBaseService;
import com.quyeying.service.TblUserService;
import com.quyeying.utils.result.R;
import com.quyeying.utils.result.ResultEnum;
import com.quyeying.utils.wx.PayUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author shenkaixin
 */
@Slf4j
@RestController
@RequestMapping("/wx")
public class WxController {

    @Resource
    ObjectMapper objectMapper;
    @Resource
    TblUserService userService;
    @Resource
    TblOrderBaseService orderBaseService;

    /**
     * 登录接口
     * @param code
     * @return
     */
    @PostMapping("/login/{code}")
    public R login(@PathVariable String code) {
        try {
            return userService.wxLogin(code);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return R.error(ResultEnum.SYSTEM_ERROR);
    }

    /**
     * 付款
     */
    @PostMapping("/wxPay")
    public R wxPay(HttpServletRequest request, @RequestBody Map<String, Object> params) {
        return orderBaseService.createOrder(request, params);
    }

    /**
     * 付款回调
     */
    @PassToken
    @PostMapping("/notifyUrl")
    public JSONObject notifyUrl(HttpServletRequest request) {
        String body = PayUtil.readData(request);
        log.info("body==="+body);
        //回调通知的验签与解密
        String wechatPaySerial = request.getHeader("Wechatpay-Serial");
        String nonce = request.getHeader("Wechatpay-Nonce");
        String timestamp = request.getHeader("Wechatpay-Timestamp");
        String signature = request.getHeader("Wechatpay-Signature");

        JSONObject jsonObject = new JSONObject();
        //解密报文
        try {
            String decryptOrder = PayUtil.signVerificationAndDecryption(wechatPaySerial, nonce, timestamp, signature, body);
            orderBaseService.processPaymentSuccessOrder(objectMapper.readTree(decryptOrder));
        } catch (RuntimeException | JsonProcessingException e) {
            e.printStackTrace();
            jsonObject.put("code","ERROR");
            jsonObject.put("message","失败");
            return jsonObject;
        }

        jsonObject.put("code","SUCCESS");
        jsonObject.put("message","成功");
        return jsonObject;
    }

    /**
     * 退款回调
     */
    @PassToken
    @PostMapping("/refundNotifyUrl")
    public JSONObject refundNotifyUrl(HttpServletRequest request) {

        String body = PayUtil.readData(request);
        //回调通知的验签与解密
        String wechatPaySerial = request.getHeader("Wechatpay-Serial");
        String nonce = request.getHeader("Wechatpay-Nonce");
        String timestamp = request.getHeader("Wechatpay-Timestamp");
        String signature = request.getHeader("Wechatpay-Signature");

        JSONObject jsonObject = new JSONObject();
        try {
            //解密报文
            String decryptOrder = PayUtil.signVerificationAndDecryption(wechatPaySerial, nonce, timestamp, signature, body);
            //后期完善逻辑
        } catch (RuntimeException e) {
            jsonObject.put("code","ERROR");
            jsonObject.put("message","失败");
            return jsonObject;
        }

        jsonObject.put("code","SUCCESS");
        jsonObject.put("message","成功");
        return jsonObject;
    }
}
